package crl.encrypt.ignores
{
	import com.swfwire.decompiler.abc.ABCFile;
	import com.swfwire.decompiler.abc.tokens.StringToken;
	
	import crl.encrypt.ReplaceCoder;
	
	import flash.utils.Dictionary;
	
	/**
	 * 
	 * @author crl
	 * 
	 */	
	public class StringIgnoreInfo extends AbsIgnore
	{
		private var tokenListDic:Vector.<StringToken>;
		private var totalString:Dictionary;
		public function StringIgnoreInfo()
		{
		}
		
		override public function reset(replaceCoder:ReplaceCoder):void{
			this.replaceCoder=replaceCoder;
		
			totalString=replaceCoder.totalStrings;
			tokenListDic=new Vector.<StringToken>();
		}
		
		override public function start(abc:ABCFile):void{
			var cpoolString:Vector.<StringToken>=abc.cpool.strings;
			
			var value:String;
			var token:StringToken;
			
			var i:int;
			
			var pIndex:int;
			var xIndex:int;
			var dIndex:int;
			for each(token in cpoolString){
				value=token.utf8;
				if(!value)continue;
				
				if(isNaN(Number(value))==false)continue;
				
				pIndex=value.indexOf("."); 
				xIndex=value.indexOf("/");
				dIndex=value.indexOf(":");
				if(pIndex!=-1 || xIndex !=-1 || dIndex !=-1){
					
					if(totalString[value])continue;
					
					tokenListDic.push(token);
					
					//trace(value,i,totalString.indexOf(value));
					continue;
				}
				
				/*if(value.indexOf(".") !=-1){
					tokenListDic[token]=value.split(".");
					continue;
				}*/
				
				token.utf8=replaceCoder.replaceName(value);
				i++;
			}
			
		}
		
		override public function end():void{
			
			var token:StringToken;
			var len:int,i:int;
			var list:Array;
			var value:String;
			
			var temp:Array;
			var replacedDic:Dictionary=replaceCoder.replacedDic;
			for each(token in tokenListDic){
				value=splice(token.utf8,"/");
			
				token.utf8=value;
			}
		}
		
		
		public function splice(value:String,spV:String="/"):String{
			
			
			if(value.indexOf("/")==-1 && value.indexOf(":")==-1){
				return replaceCoder.replaceName(value);
			}
			
			
			
			var lenA:int,lenB:int,i:int,j:int;
			var list:Array;
			var value:String;
			var char:String;
			var tempA:Array,tempB:Array;
			
			tempA=value.split(spV);
			
			lenA=tempA.length;
			
			var chatLen:int;
			for(i=0;i<lenA;i++){
				
				value=tempA[i];
			
				tempB=value.split(":");
				
				lenB=tempB.length
				for(j=0;j<lenB;j++){
					tempB[j]=replaceCoder.replaceName(tempB[j]);
				}
				
				tempA[i]=tempB.join(":");
			}
			
			
			return tempA.join("/");
		}
		
		
	}
}